import { Component, OnInit } from '@angular/core';
import { FadeInTop } from "../../shared/animations/fade-in-top.decorator";
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../shared/smartadmin.config';
declare var $: any;
@FadeInTop()
@Component({
	selector: 'sa-data-map',
	templateUrl: './data-map.component.html',
	styleUrls: ['./data-map.css']
})
export class DataMapComponent implements OnInit {
	constructor(private jsonApiService: JsonApiService) {}
	btnData = []; //权限按钮集合
	statsDrawAD = false; //是否有权限(按钮)
	isAllowPre: boolean = false;
	isAllowNext: boolean = false;
	startTime = '';
	endTime = '';
	carNumber = '';
	deviceId = ''; //设备ID
	selectedCompanyName = '';
	selectedCompanyId = '';
	dateNumber = 0; //查看天数
	//地图折线组件所需参数 start
	chartlineParams: any;
	isMapView = false; //折线图子组件选中是否展开地图
	drawTrackArr = [];
	specifyTime: any;
	dataMapAuthority: string = 'notAllow'; //数据绘图权限标识
	//地图折线组件方法 start
	runParent(value) {
		// 接收子组件传递过来的数据 
		this.drawTrackArr = value;
	}
	//时间选中回调
	getSelectChartTime(time) {
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	}
	//公司选中回调
	companySelected(val) {
		this.deviceId = val.deviceId;
		this.carNumber = val.carNumber;
		this.selectedCompanyId = val.companyId;
		this.selectedCompanyName = val.companyName;
	}
	showMapView(val) {
		this.isMapView = val;
	}
	//获取传感器通道参数
	lineParamDeal(){
		let reqdata = 'deviceId=' + this.deviceId;
		this.jsonApiService.http("realtime/getRealTimeData.html", reqdata).subscribe(result => {
			if(result.success&&result.data){
				let params = {
					deviceId: this.deviceId,
					startTime: this.startTime,
					endTime: this.endTime,
					coefficient:result.data.coef
				}
				this.chartlineParams = params;
			}
		})
	}
	//数据绘图
	showChart() {
		if(!this.startTime || !this.endTime) {
			this.jsonApiService.message('起始结束时间不能为空');
			return;
		}
		let timeCheck = common.correctTimeParams(this.startTime, this.endTime, 0);
		if(!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		this.lineParamDeal();
	}
	ngOnInit() {
		this.initBtn();
		this.dateNumber = 1;
		this.selectDate();

	}
	initBtn() {
		this.btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_stats:draw')));
		if(!this.btnData) {
			this.dataMapAuthority = 'notAllow';
			return;
		}
		for(let item of this.btnData) {
			if(common.stringTrim(item.sourceKey) === "stats:drawAD" && item.parentId) {
				this.statsDrawAD = true;
				this.dataMapAuthority = 'allow';
			}
		}
	}

	del_date() {
		$('#datetimepicker1').val('');
		$('#datetimepicker2').val('');
		this.startTime = '';
		this.endTime = '';

	}
	selectDate() {
		let nowTime = new Date();
		this.endTime = common.getTimeMmss(nowTime);
		nowTime.setDate(nowTime.getDate() - this.dateNumber);
		this.startTime = common.getTimeMmss(nowTime);
		this.specifyTime = {
			startTime: this.startTime,
			endTime: this.endTime,
		}
	}
}